package com.sdx.console.promote.web;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.ibeetl.admin.core.annotation.Function;
import com.ibeetl.admin.core.entity.CoreUser;
import com.ibeetl.admin.core.service.CorePlatformService;
import com.ibeetl.admin.core.util.DateUtil;
import com.ibeetl.admin.core.util.ValidateConfig;
import com.ibeetl.admin.core.web.AuthorizedBaesController;
import com.ibeetl.admin.core.web.JsonResult;
import com.sdx.common.utils.JacksonUtils;
import com.sdx.common.utils.Utils;
import com.sdx.console.promote.entity.PromoteLink;
import com.sdx.console.promote.service.PromoteLinkService;
import com.sdx.console.promote.service.TemplateShareService;
import org.beetl.sql.core.engine.PageQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 推广链接 接口
 */
@RequestMapping("/promote/promoteLink")
@Controller
public class PromoteLinkController extends AuthorizedBaesController{
	private static final Logger log = LoggerFactory.getLogger(PromoteLinkController.class);
    private static final String MODEL = "/promote/promoteLink";


    @Resource
	private PromoteLinkService promoteLinkService;
    
    @Resource
    private TemplateShareService templateShareService;

    private void setPageParam(ModelAndView view) {
		view.addObject("templateIdList",templateShareService.selectAll());
    }

    /* 页面 */

    @GetMapping("/index.do")
    @Function("promote.promoteLink.query")
    @ResponseBody
    public ModelAndView index() {
        ModelAndView view = new ModelAndView(MODEL+"/index.html") ;
        return view;
    }

    @GetMapping("/edit.do")
    @Function("promote.promoteLink.edit")
    @ResponseBody
    public ModelAndView edit(Integer linkId) {
        ModelAndView view = new ModelAndView(MODEL+"/add.html");
        PromoteLink promoteLink = promoteLinkService.queryById(linkId);
	    setPageParam(view);
        view.addObject("promoteLink", promoteLink);
		try {
			view.addObject("promoteLinkJson", JacksonUtils.toJsonString(promoteLink,JacksonUtils.DF1));
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
        return view;
    }

    @GetMapping("/add.do")
    @Function("promote.promoteLink.add")
    @ResponseBody
    public ModelAndView add() {
        ModelAndView view = new ModelAndView(MODEL+"/add.html");
	    setPageParam(view);
        return view;
    }

    /* ajax json */

    @PostMapping("/list.json")
    @Function("promote.promoteLink.query")
    @ResponseBody
    public JsonResult<PageQuery> list(PromoteLink condtion,HttpServletRequest request)
    {
        CoreUser user = getLoginUser(request);
        condtion.setTenantId(user.getTenantId());
        PageQuery page = condtion.getPageQuery();
        promoteLinkService.queryByCondition(page);
        return JsonResult.success(page);
    }

    private JsonResult save(boolean isNew,PromoteLink promoteLink,HttpServletRequest request
	) {
    	JsonResult result = JsonResult.success();
		String dir = DateUtil.now("yyyyMMdd");
    	String subPath = "/images/cover/"+dir+"/";
		String bucketName = "sdxcomm";
		CoreUser user = getLoginUser(request);

    	if (isNew) {
            promoteLink.setTenantId(user.getTenantId());
			promoteLink.setCreatedBy(user.getId().intValue());
			promoteLink.setCreatedName(user.getName());
			promoteLink.setCreatedTime(new Date());
			promoteLinkService.save(promoteLink);
		} else {
			promoteLink.setUpdatedBy(user.getId().intValue());
			promoteLink.setUpdatedName(user.getName());
			promoteLink.setUpdatedTime(new Date());
			promoteLinkService.updateTemplate(promoteLink);
		}
		return result;
	}
    @PostMapping("/add.json")
    @Function("promote.promoteLink.add")
    @ResponseBody
    public JsonResult add(@Validated(ValidateConfig.ADD.class)PromoteLink promoteLink,HttpServletRequest request)
    {

        return save(true,promoteLink,request);
    }

    @PostMapping("/update.json")
    @Function("promote.promoteLink.edit")
    @ResponseBody
    public JsonResult<String> update(@Validated(ValidateConfig.UPDATE.class)  PromoteLink promoteLink,HttpServletRequest request
		) {
		return save(false,promoteLink,request
		);
    }


   
    @GetMapping("/view.json")
    @Function("promote.promoteLink.query")
    @ResponseBody
    public JsonResult<PromoteLink>queryInfo(Integer linkId) {
        PromoteLink promoteLink = promoteLinkService.queryById( linkId);
        return  JsonResult.success(promoteLink);
    }

    @PostMapping("/delete.json")
    @Function("promote.promoteLink..del")
    @ResponseBody
    public JsonResult delete(String ids) {
		List<String> idList = Utils.strToList(ids,",");
        promoteLinkService.batchDelPromoteLink(idList);
        return JsonResult.success();
    }
    

}
